package com.hs.pk.berth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.common.api.dto.PageBean;
import com.hs.common.api.dto.ResultCode;
import com.hs.common.api.dto.ResultVo;
import com.hs.pk.berth.bean.Berth;
import com.hs.pk.berth.bean.Road;
import com.hs.pk.berth.dto.BerthDto;
import com.hs.pk.berth.dto.FindBerthDto;
import com.hs.pk.berth.mapper.RoadMapper;
import com.hs.pk.berth.service.IRoadService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hu.pk.patroller.client.PatrollerClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 路段 服务实现类
 * </p>
 *
 * @author lhj
 * @since 2022-07-20
 */
@Service
@Slf4j
public class RoadServiceImpl extends ServiceImpl<RoadMapper, Road> implements IRoadService {

    @Autowired
    private RoadMapper roadMapper;
    @Autowired
    private PatrollerClient patrollerClient;

    @Override
    public ResultVo findRoadByPage(Integer current, Integer size, FindBerthDto findBerthDto) {

        System.out.println("berthDto = " + findBerthDto);


        QueryWrapper<Road> qw = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(findBerthDto)) {
            //1.berthDto对象不为空
            //1.1 根据路段名称或路段收费类型，进行条件查询
            Map<String, String> map = new HashMap<>();
            map.put("name", findBerthDto.getRoadName());
            map.put("type", findBerthDto.getRoadType());

            //1.2 判断远程调用的条件是否为空
            if (StringUtils.hasText(findBerthDto.getPatrollerName())) {
                // 根据巡检员名称 远程调用 巡检员表查询对应路段信息
                Long roadId = patrollerClient.selectRoadIdByPatrollerName(findBerthDto.getPatrollerName());
                log.info("路段Id：{}", roadId);
                map.put("id", roadId.toString());
            }
            qw.allEq(map, false);
        }
        Page<Road> p = new Page<>(current, size);
        Page<Road> page = roadMapper.selectPage(p, qw);
        Long total = page.getTotal();
        List<Road> records = page.getRecords();
        System.out.println("records = " + records);

        if (!CollectionUtils.isEmpty(records)) {
            PageBean pageBean = new PageBean();
            pageBean.setTotals(total.intValue());
            pageBean.setList(records);

            return new ResultVo(pageBean);
        }
        return new ResultVo(ResultCode.FAILED);

    }

    //
    @Override
    public List<Road> findRoadByStatus(Integer status) {
        LambdaQueryWrapper<Road> qw = Wrappers.lambdaQuery(Road.class);
        qw.select(Road::getId, Road::getImitBerthNum);
        qw.eq(Road::getStatus, status);
        List<Road> list = this.list(qw);
        return list;
    }

    @Override
    public BerthDto searchBerthDtoByRoadName(String roadName) {
        BerthDto berthDto = roadMapper.selectBerthDtoByroadName(roadName);
        return berthDto;


    }
}
